# _*_ coding: utf-8 _*_
from odoo import api, fields, models, _


class ZfyPicking(models.Model):
    _inherit = "stock.picking"

    @api.multi
    def do_new_transfer(self):
        # pick_qty1 = 0
        # order_qty1 = 0
        # del_qty = 0
        # for pick in self:
        #     if pick.move_lines:
        #         for move_qty in pick.move_lines:
        #             a = move_qty.product_qty
        #             pick_qty1 += a
        #     if pick.sale_id:
        #         for order_qty in pick.sale_id.order_line:
        #             b = order_qty.product_uom_qty
        #             c = order_qty.qty_delivered
        #             order_qty1 += b
        #             del_qty += c
        #         if (pick_qty1+del_qty) >= order_qty1:
        #             pick.sale_id.state = 'done'
        # for pick in self:
        #     if (pick.move_lines and (pick.state != 'Cancelled')):
        #         flag = 0
        #         for stkMv in pick.move_lines:
        #             a = stkMv.product_qty  # 本次移动的数量
        #             b = stkMv.procurement_id.sale_line_id.product_uom_qty  # 取得销售订单行的数量
        #             c = stkMv.procurement_id.sale_line_id
        #             if pick.sale_id:
        #                 for salOrdLine in pick.sale_id.order_line:
        #                     if salOrdLine == c:
        #                         d = salOrdLine.qty_delivered + a  # 已出货数量+本次出货量
        #                         if d < b:
        #                             flag = 1
        #                             break
        #         if flag == 0:
        #             pick.sale_id.state = 'done'
        #return super(ZfyPicking, self).do_new_transfer()
        res = super(ZfyPicking, self).do_new_transfer()
        for pick in self:
            if pick.sale_id:
                flag = 0
                for salOrdLine in pick.sale_id.order_line:
                    a = salOrdLine.qty_delivered
                    b = salOrdLine.product_uom_qty
                    if a<b:
                        flag = 1
                        break
                if flag == 0:
                    pick.sale_id.state = 'done'
        return res
